//
//  SKWebViewController.swift
//  ScarnKing
//
//  Created by ninebot on 2025/7/28.
//

import Foundation
import UIKit
import WebKit

class SKWebViewController: UIViewController,WKUIDelegate,WKNavigationDelegate {
    var webView: WKWebView?
    var titleLabel:UILabel?
    public var webUrl: String?
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .black
        let topView = UIView(frame: CGRect(x: 0, y: -UIApplication.shared.statusBarFrame.height, width: kScreenW, height: UIApplication.shared.statusBarFrame.height))
        topView.backgroundColor = .black
        view.addSubview(topView)
        
        titleLabel = UILabel.init(frame: CGRect(x: 0, y: UIApplication.shared.statusBarFrame.height, width: kScreenW, height: 49))
        titleLabel!.textColor = UIColor.white
        titleLabel!.textAlignment = .center
        titleLabel!.backgroundColor = .clear
        view.addSubview(titleLabel!)
        
        let backButton = UIButton.init(frame: CGRect.init(x: 0, y: UIApplication.shared.statusBarFrame.height, width: 40, height: 40))
        backButton.setImage(UIImage.init(named: "login_back_right"), for: .normal)
        backButton.addTarget(self, action: #selector(backButtonClick), for: .touchUpInside)
//        backButton.backgroundColor = .red
        view.addSubview(backButton)

        webView = WKWebView(frame: CGRect(x: 0, y: UIApplication.shared.statusBarFrame.height + 50, width: kScreenW, height: kScreenH - 64))
        webView?.backgroundColor = .white
        webView?.uiDelegate = self
        webView?.navigationDelegate = self
        webView?.addObserver(self, forKeyPath: "title", options: NSKeyValueObservingOptions.new, context: nil)


        let infoDictionary = Bundle.main.infoDictionary!
        let minorVersion = infoDictionary["CFBundleShortVersionString"]//版本号（内部标示）

        if webUrl == nil{

            webUrl = "http://ikanshu-app-ios-h5.51ikanshu.cn/newAbout?version="
             let appVersion = webUrl! + (minorVersion as! String)
            let request = NSMutableURLRequest(url: URL(string: appVersion)!, cachePolicy: .reloadIgnoringLocalCacheData, timeoutInterval: 1.0)
                   webView?.load(request as URLRequest)
        }else{

            var appVersion = webUrl!
            appVersion = appVersion.trimmingCharacters(in: .whitespaces)
            let request = NSMutableURLRequest(url: URL(string: appVersion)!, cachePolicy: .useProtocolCachePolicy, timeoutInterval: 10.0)
            webView?.load(request as URLRequest)

          //  NSURLRequest * request = [[NSURLRequest alloc]initWithURL:[NSURL URLWithString:self.url] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:600.0f];

        }

        
        self.view.addSubview(webView!)
    }
    
    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
        if navigationAction.navigationType == WKNavigationType.linkActivated {
            UIApplication.shared.openURL(navigationAction.request.url!)
            decisionHandler(WKNavigationActionPolicy.cancel)
        } else {
            decisionHandler(WKNavigationActionPolicy.allow)
        }
        return
    }
 


    override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?){
           if keyPath == "title" {
               if object as? WKWebView == webView {
                //title = webView?.title ?? ""
                titleLabel?.text = webView?.title ?? ""
               }
           }
       }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        self.navigationController?.navigationBar.isHidden = true;
        self.tabBarController?.tabBar.isHidden = true

    }
    
    override func viewWillAppear(_ animated: Bool) {
        //        Account.requestUserInfo()
         super.viewWillAppear(animated)
        self.navigationController?.navigationBar.isHidden = true;
        self.tabBarController?.tabBar.isHidden = true

    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        // 恢复底部标签栏
        self.tabBarController?.tabBar.isHidden = false
        self.navigationController?.navigationBar.isHidden = true;
    }
    
    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        // 确保底部标签栏显示
        self.tabBarController?.tabBar.isHidden = false
        self.navigationController?.navigationBar.isHidden = true;
    }
     

    
    @objc func backButtonClick() {

            self.navigationController?.popViewController(animated: false)


        }

    }

